//
//  EPAttachItem.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-1-23.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EPAttachItem.h"
#import "UIView+Addtions.h"
#import <QuartzCore/QuartzCore.h>

#define ItemWidth (CurrentDeviceISiPad ? 160 : 80)
#define ItemHeight (CurrentDeviceISiPad ? 208 : 104)
#define ItemButtonPadding (CurrentDeviceISiPad ? 4 : 2)
#define ItemButtonSizeValue (CurrentDeviceISiPad ?  152: 76)

@interface EPAttachItem ()

@property (nonatomic, strong) UIButton *itemButton;

@end

@implementation EPAttachItem

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithDefaultStyle {
    self = [super initWithFrame:CGRectMake(0, 0, ItemWidth, ItemHeight)];
    if (self) {
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews {
    self.itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_itemButton addTarget:self action:@selector(itemButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_itemButton];
    
    self.titleLabel = [[UILabel alloc] init];
    [self addSubview:_titleLabel];
    
    self.sizeLabel = [UILabel.alloc init];
    [self addSubview:_sizeLabel];
    
    [self addDefaultProperty];
}

- (void)addDefaultProperty {

    _titleLabel.font = [UIFont systemFontOfSize:14];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    //_titleLabel.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
    
    _sizeLabel.font = [UIFont systemFontOfSize:12];
    _sizeLabel.textAlignment = NSTextAlignmentCenter;
    //_sizeLabel.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.6];
    _sizeLabel.textColor = [UIColor grayColor];
}


- (void)layoutSubviews {
    [super layoutSubviews];

    [_itemButton setImage:_itemImage forState:UIControlStateNormal];
//    [_itemButton setImage:_itemImage forState:UIControlStateHighlighted];
    _itemButton.frame = CGRectMake(ItemButtonPadding, ItemButtonPadding, ItemButtonSizeValue, ItemButtonSizeValue);
    
    [_titleLabel sizeToFit];
    _titleLabel.frame = CGRectMake(0, _itemButton.bottom, self.width, _titleLabel.height);
    
    [_sizeLabel sizeToFit];
    _sizeLabel.frame = CGRectMake(0, _titleLabel.bottom, self.width, _sizeLabel.height);
}

- (void)itemButtonPressed:(UIButton *)button {
    if (_delegate && [_delegate respondsToSelector:@selector(itemSelected:)]) {
        [_delegate itemSelected:self];
    }
}

@end
